<template>
  <div>
    <el-upload
      :disabled="isUpload"
      class="avatar-uploader"
      action="#"
      :show-file-list="false"
      :http-request="upload"
    >
      <img
        v-if="urlImg"
        :src="urlImg"
        class="avatar"
      >
      <i class="el-icon-circle-close">{{urlImg ? '' : '上传图片'}}</i>
      <span class="icon" @click.stop="delImg"></span>
    </el-upload>
  </div>
</template>

<script>
// 实例化cos对象
const COS = require('cos-js-sdk-v5')
// 填写自己腾讯云cos中的key和id (密钥)
const cos = new COS({
  SecretId: 'AKID3wZw2sRjlW0D9oCJAk23nH2WjgMkrpXy', // 身份识别ID
  SecretKey: 'B4g15sOdcJkunPkob6KIzPXP0LPBhwua' // 身份秘钥
})
export default {
  name: 'uploadImg',
  props: {
    value: {
      type: String,
      required: ''
    }
  },
  data () {
    return {
      urlImg: this.value,
      isUpload: false
    }
  },
  watch: {
    value () {
      this.urlImg = this.value
    }
  },
  methods: {
    upload (res) {
      if (res.file) {
        cos.putObject({
          Bucket: 'rlzy-one-1306404223', /* 存储桶 */
          Region: 'ap-beijing', /* 存储桶所在地域，必须字段 */
          Key: res.file.name, /* 文件名 */
          StorageClass: 'STANDARD', // 上传模式, 标准模式
          Body: res.file // 上传文件对象
        }, (err, data) => {
          console.log(err || data)
          if (data.statusCode === 200) {
            this.urlImg = `https:${data.Location}`
            this.$emit('input', this.urlImg)
          }
        })
      }
    },
    // 点击删除
    delImg () {
      this.isUpload = true
      this.urlImg = ''
      setTimeout(() => {
        this.isUpload = false
      })
    }
  }
}
</script>
<style lang="scss" scoped>
/* .update {
  display: inline-block;
  position:relative;
  left:4px;
  top: -35px;
}
.upload-demo /deep/ .el-upload--picture-card{
  height:60px;
  width:100px;
}
.el-icon-plus:before {
  content: "上传图片";
}
.upload-demo /deep/ .el-icon-upload {
  position:absolute;
  top:40%;
  right:15%;
  font-size: 14px;
  color:#303133;
} */
.avatar-uploader {
  display: inline-block;
  vertical-align: middle;
  margin-left: 4px;
}
.el-checkbox-group .imgFile {
  float: none !important;
  display: inline-block;
  // margin-bottom: 20px;
}
// .options .el-checkbox-group div {
//   margin-bottom: 20px !important;
// }
/* padding-bottom:10px; */
::v-deep.avatar-uploader .el-upload {
  width: 100px;
  height: 60px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  // overflow: hidden;
}
::v-deep.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
::v-deep.el-icon-upload {
  font-size: 14px;
  color: #8c939d;
  width: 100px;
  height: 60px;
  line-height: 60px;
  text-align: center;
}
::v-deep.avatar {
  width: 100px;
  height: 60px;
  display: block;
}
::v-deep.progress {
  position: absolute;
  top: 1px;
  left: 1px;
}
::v-deep.el-icon-upload::before {
  color: #409eff !important;
  position: absolute;
  height: 16px;
  display: inline-block;
  top: -7px;
  right: -1000px;
}
.el-icon-circle-close:before {
  font-size:16px;
  position: absolute;
  top: -7px;
  right: -7px;
  color: #8c939d;
  background-color: #fff;
}
.el-icon-circle-close{
  font-size:14px;
  margin-top:23px;
  color: #8c939d;
}
.icon {
  position: absolute;
  top: -7px;
  right: -7px;
  width: 16px;
  height: 16px;
}
</style>
